package com.aaa.dem1105.第二遍;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/11/5  17:53
 */
public class Test2 {

    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("欧阳雪",18,"中国",'F'));
        personList.add(new Person("Tom",24,"美国",'M'));
        personList.add(new Person("Harley",22,"英国",'F'));
        personList.add(new Person("向天笑",20,"中国",'M'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("小梅",20,"中国",'F'));
        personList.add(new Person("何雪",21,"中国",'F'));
        personList.add(new Person("李永",22,"中国",'M'));

        /**
         *   filter 筛选
         */
        //筛选出年龄大于20
//        personList.stream().filter(item -> item.getAge() > 20).forEach(item-> System.out.println(item));
        personList.stream().filter(item -> item.getAge() > 20).forEach(System.out::println);
        System.out.println("**********************************111111");

        /**
         *  map 映射
         */
        personList.stream().map(item -> item.getAge() > 20).forEach(System.out::println);
        System.out.println("**********************************222222");

        /**
         *  sort排序
         */
        personList.stream().sorted((o1, o2) -> Integer.compare(o1.getAge(),o2.getAge())).forEach(System.out::println);
        System.out.println("**********************************3333333");

        /**
         * find操作--findFirst和findAny
         */
        //find操作-- findFirst 获取流中第一个元素
        Optional<Person> first = personList.stream().findFirst();
        System.out.println(first.get());
        //find操作---findAny() 获取流中任意一个元素  必须使用并行流测试
        Optional<Person> any = personList.parallelStream().findAny();
        System.out.println(any.get());
        System.out.println("**********************************444444");

        /**
         *  match 匹配操作--allMatch anyMatch
         */
        //匹配操作--所有元素都必须匹配条件 才返回true
        boolean b = personList.stream().allMatch(item -> item.getAge() > 10);
        System.out.println("b--"+b);
        boolean c = personList.stream().anyMatch(item -> item.getAge() > 50);
        System.out.println("c--"+c);
        System.out.println("**********************************5555555");

        /**
         * 聚合操作 max  min count
         */
        //聚合操作---count
        long count = personList.stream().count();
        System.out.println("个数:"+count);
        //聚合操作---max min
        Optional<Person> max = personList.stream().max((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println(max.get());
        System.out.println("**********************************6666666");

        /**
         * collect搜集操作
         */
        //计数   count
        Long collect =personList.stream().collect(Collectors.counting());
        System.out.println("collect--"+"个数："+collect);
        //平均值： averagingInt、 averagingLong、 averagingDouble
        Double collect1 = personList.stream().collect(Collectors.averagingInt(item -> item.getAge()));
        System.out.println("collect1--"+"平均数："+collect1);
        //最值： maxBy、 minBy
        Optional<Person> collect2 = personList.stream().collect(Collectors.maxBy(((o1, o2) -> o1.getAge() - o2.getAge())));
        System.out.println("collect2--"+"最值："+collect2);
        //求和： summingInt、 summingLong、 summingDouble
        Integer collect3 = personList.stream().collect(Collectors.summingInt(item -> item.getAge()));
        System.out.println("collect3--"+"和："+collect3);
        //搜集为一个新的集合。
        //部分信息的
        Set<String> set = personList.stream().filter(item -> item.getAge() > 20)
                .filter(item -> item.getCountry().equals("中国"))
                .map(item -> item.getName())
                .collect(Collectors.toSet());
        System.out.println(set);
        //全部信息的
        Map<String, Person> map = personList.stream().filter(item -> item.getAge() > 20)
                .filter(item -> item.getCountry().equals("中国"))
                .collect(Collectors.toMap(item -> item.getName(), item -> item));
        System.out.println(map);
        //分组操作。
        //以性别分组
        Map<Character, List<Person>> collect4 = personList.stream().collect(Collectors.groupingBy(item -> item.getSex()));
        System.out.println("collect4--"+collect4);
    }
}

class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}
